# Maintainer: Justin Kromlinger <hashworks@archlinux.org>

pkgname=vector
pkgver=0.32.2
pkgrel=1
pkgdesc="A high-performance observability data pipeline"
arch=("loong64" "x86_64")
_target="x86_64-unknown-linux-gnu"
url="https://vector.dev"
license=("MPL2")
options=(!lto) # TODO: Build with LTO
backup=(
    "etc/vector/vector.toml"
)
depends=("gcc-libs" "zlib")
# https://github.com/vectordotdev/vector/blob/master/docs/DEVELOPING.md#bring-your-own-toolbox
makedepends=(
    "rustup"
    "protobuf"
    "python"
    "perl"
    "cmake"
    "git"
)
checkdepends=(
    "cargo-nextest"
)
source=(
    "git+https://github.com/vectordotdev/vector#tag=v${pkgver}"
    "${pkgname}.sysusers"
    "${pkgname}.tmpfiles"
)
sha512sums=('SKIP'
            '4f7a5d63f4e89018d1f0e9aa0fba2bb5c448d7031a7ff40c82878f574243e075c2fca020e78c4c71b1caa1a5bd1f06b496a5d3ab432f01d145233fd6c9ec4b87'
            'c192492df09d131f9174d2acbb7f265c280eb6d678589b8c93bacc47ae55c51573a5477d715897f8580ced420730992fb68bee78b374f1cc042888ea6b5512fd')

pkgver() {
  cd "${pkgname}"
  git describe --tags --match 'v*' | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
    cd "${pkgname}"

    cargo fetch \
        --locked
}

build() {
    cd "${pkgname}"
    # --frozen fails atm due to "Unable to update https://github.com/vectordotdev/openssl-src-rs.git?tag=release-300-force-engine+3.1.2"
    # Should we provide that manually?
    cargo build \
        --release \
        --locked \
        --target "${_target}"

    # Since cargo nextest drops our release build, we need to back it up
    cp -r target/${_target}/release releasebuild
}

check() {
    cd "${pkgname}"

    # Unit-Tests only, integration tests require services
    cargo nextest run \
        --workspace \
        --fail-fast \
        --test-threads num-cpus \
        --frozen \
        --release \
        --locked \
        --offline \
        --no-default-features \
        --target "${_target}"
}

package() {
    install -Dm644 "${pkgname}.sysusers" "${pkgdir}/usr/lib/sysusers.d/${pkgname}.conf"
    install -Dm644 "${pkgname}.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/${pkgname}.conf"

    cd "${pkgname}"

    for f in LICENSE NOTICE; do
        install -Dm644 "$f" "${pkgdir}/usr/share/licenses/${pkgname}/$f"
    done

    for f in CHANGELOG PRIVACY README RELEASES SECURITY VERSIONING; do
        install -Dm644 "$f.md" "${pkgdir}/usr/share/doc/${pkgname}/$f.md"
    done

    install -Dm755 "releasebuild/vector" "${pkgdir}/usr/bin/vector"

    install -Dm644 "config/vector.toml" "${pkgdir}/etc/vector/vector.toml"
    cp -r config/examples "${pkgdir}/usr/share/doc/${pkgname}/examples"

    install -Dm644 "distribution/systemd/vector.service" "${pkgdir}/usr/lib/systemd/system/vector.service"
    install -Dm644 "distribution/systemd/hardened-vector.service" "${pkgdir}/usr/lib/systemd/system/hardened-vector.service"
    install -Dm644 "distribution/systemd/vector.default" "${pkgdir}/etc/default/vector"
}
